<template>
  <div class="productListWait">
    <div class="container">

      <createTop firstMenu="出库管理" secondMenu="辅件出库" title="出库详情"></createTop>

      <!-- 第二个表格 -->
      <el-card class="top-card" shadow="always">
        <div slot="header" class="card-title">
          <span>待出库清单</span>
          <el-button
          :style="{'marginLeft':'20px'}"
          size="mini"
          plain
          type="success"
          @click="add">新增</el-button>
        </div>
        <!-- <tableList :tableData="listData" @add='add' @del='del'></tableList> -->
           <div class="productListWait-table-wrap">
              <el-table
                :data="tableData"
                style="width: 100%"
                :fit="true"
                :stripe="true"
                size="mini"
                :border="true"
                align="center">

                <el-table-column label="操作"
                min-width="140"
                align="center">
                  <template slot-scope="scope">
                    <!-- <el-button
                      size="mini"
                      plain
                      type="primary"
                      v-if="scope.$index===0"
                      @click="add">新增</el-button> -->
                    <el-button
                      size="mini"
                      plain
                      type="danger"
                      @click="del(scope.$index,scope.row)">删除</el-button>
                  </template>
                </el-table-column>

                <el-table-column
                  prop="number"
                  label="配料编号"
                  min-width="100"
                  align="center">
                </el-table-column>

                <el-table-column
                  prop="auxiliary_parts_name"
                  label="辅件名称"
                  min-width="130"
                  align="center">
                </el-table-column>

                <el-table-column
                  prop="auxiliary_parts_formats"
                  label="规格/型号"
                  min-width="130"
                  align="center">
                </el-table-column>

                <el-table-column
                  prop="unit"
                  label="单位"
                  min-width="120"
                  align="center">
                </el-table-column>

                <el-table-column
                  prop="kucun_count"
                  label="库存数量"
                  min-width="120"
                  align="center">
                </el-table-column>
                
                <el-table-column 
                label="出库数量"
                min-width="180"
                align="center">
                  <template slot-scope="scope">
                    <el-input 
                    v-model="scope.row.outCount" 
                    @blur="changeOutCount(scope)"
                    placeholder="输入出库数量"></el-input>
                  </template>
                </el-table-column>

                <el-table-column 
                label="备注"
                min-width="180"
                align="center">
                  <template slot-scope="scope">
                    <el-input 
                    v-model="scope.row.tableRemark" 
                    placeholder="填写备注"></el-input>
                  </template>
                </el-table-column>
              </el-table>
              <deleteConfirm v-if="showMask" @closeDelete="closeDelete" @deleteConfirm="deleteConfirm"></deleteConfirm>
            </div>
      </el-card>

      <el-card shadow="always">
        <div slot="header" class="card-title">
          <span>出库信息</span>
        </div>
        <div class="content">
          <div class="se">
            <div class="content-title">制单人</div>
            <el-input :style="{'width':'350px'}" disabled v-model="doName" placeholder="读取账号"></el-input>
          </div>

          <div class="se">
            <div class="content-title">出库单号 </div>
            <el-input :style="{'width':'350px'}" disabled v-model="rukuid" placeholder="自动填充"></el-input>
          </div>
        </div>

        <div class="content">
          <div class="se">

            <div class="content-title">出库日期 <span class="red-icon">※</span></div>
            <el-date-picker
              v-model="date"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd">
            </el-date-picker>

            <div class="content-title">出库类型 </div>
            <el-select :style="{'width':'350px'}" v-model="state" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>

            <div class="content-title">经办人 <span class="red-icon">※</span> </div>
            <el-input :style="{'width':'350px'}" v-model="doname" placeholder="输入经办人姓名"></el-input>
          </div>

          <div class="se">
            <div class="content-title">出库备注  </div>
            <el-input :style="{'width':'350px'}" v-model="remark" type="textarea" :rows="5" resize="none" placeholder="输入适当的备注方便管理"></el-input>
          </div>
        </div>
      </el-card>
        <el-button
        class="confirms"
        size="mini"
        type="primary"
        @click="confirm">保 存</el-button>

      <childTable :id="id" v-show="showChild" @confirm="addTable" @close="closeChild"></childTable>

    </div>
  </div>
</template>
<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import createTop from '@/components/createTop.vue';
import saveBtn from '@/components/saveBtn.vue';
import ajaxResquest from '@/api/apiRequest';
import childTable from '@/tables/auxiliary/auxiliaryInCreateChild.vue';
@Component({
  components: {
    createTop,
    saveBtn,
    childTable
  }
})
export default class AuxiliaryOutCreate extends Vue {
  createRes!: ajaxResquest;

  row: any = [];

  tableData: any[] = [];

  outCount: string = '';
  tableRemark: string = '';
  showMask: boolean = false;
  leixing: string = '';
  doname: string = '';
  rukuid: string = '';
  remark: string = '';
  doName: string = '';
  date: any = '';
  showChild: boolean = false;
  $router!: any;
  id: any = '';
  state: string = '';
  options: any[] = [
    {
      value: '生产消耗',
      label: '生产消耗'
    },
    {
      value: '其它出库',
      label: '其它出库'
    },
    {
      value: '采购退货',
      label: '采购退货'
    }
  ];
  created() {
    this.id = this.$route.query.id;
    console.log(this.id);
  }
  mounted() {
    // this.row = temp;
    this.createRes = new ajaxResquest('/auxiliaryparts/auxiliaryInOutToDo');
  }
  add(val: any) {
    console.log(val);
    this.showChild = true;
  }
  del(index: number, val: any) {
    // console.log(val);
    console.log(index);
    this.tableData.splice(index, 1);
  }
  closeChild() {
    this.showChild = false;
  }
  addTable(val: any) {
    console.log(val);

    let temp = val;
    let tempData: any[] = [];
    temp.map((i: any) => {
      i._models_id.map((j: any) => {
        let name!: string;
        let fId!: string;
        let kucun_count!: string;
        i.auxiliary_parts_formats.forEach((e: any) => {
          if (e.auxiliary_parts_formats_id === j) {
            name = e.auxiliary_parts_formats_name;
            fId = e.auxiliary_parts_formats_id;
            kucun_count = e.fujian_count;
          }
        });
        let data = {
          id: i.id,
          auxiliary_parts_name: i.auxiliary_parts_name,
          number: i.number,
          unit: i.unit,
          auxiliary_parts_formats: name,
          auxiliary_parts_id: fId,
          tableRemark: '',
          outCount: 0,
          kucun_count: kucun_count
        };
        tempData.push(data);
      });
    });
    this.tableData = [...this.tableData, ...tempData];
    console.log(this.tableData);
    this.showChild = false;
  }
  changeOutCount(scope: any) {
    console.log(scope.row);
    if (scope.row.outCount > scope.row.material_all_count) {
      console.log('数量超了');
    }
  }
  confirm(): void {
    let temp = this.tableData;
    console.log(temp);
    let sTemp: any[] = [];
    let hasError: boolean = false;

    temp.map((i: any) => {
      if (parseInt(i.outCount) > parseInt(i.kucun_count)) {
        this.$message.error('出库数量超过库存量!');
        hasError = true;
      }
      if (!i.outCount) {
        this.$message.error('请填写出库数量!!');
        hasError = true;
      }
      let data = {
        format_id: i.auxiliary_parts_id,
        auxiliary_parts_id: i.id,
        number: i.outCount === '0' ? i.outCount : '-' + i.outCount,
        remarks: i.tableRemark
      };
      sTemp.push(data);
    });
    if (hasError) {
      return;
    }
    let NTemp: string = JSON.stringify(sTemp);
    console.log(NTemp);
    // return;
    let data: any = {
      doName: this.doname,
      date: this.date,
      type: 'out',
      remarks: this.remark,
      state: this.state,
      auxiliaryPartsInfo: NTemp,
      orderId: this.id
    };
    this.createRes.getFunction(data).then((res:any) => {
      if (res.data.code === 1) {
        this.$to('auxiliaryOut');
      }
    });
  }
}
</script>
<style lang="less">
.productListWait {
  height: 100%;
  padding: 15px;
  display: flex;
  flex-direction: column;
  z-index: 101;
  box-sizing: border-box;
  overflow-y: auto;
  .top-card {
    box-sizing: border-box;
    margin-bottom: 15px;
  }
  .el-card {
    overflow-y: auto;
  }
  .content {
    font-size: 16px;
    background-color: #fff;
    border-radius: 4px;
    .content-title {
      height: 40px;
      margin-top: 20px;
      line-height: 2;
      text-align: left;
    }
    .short-input {
      width: 100%;
      width: 240px;
      margin-right: 10px;
    }
    .el-upload-list__item {
      width: 350px;
    }
    .el-textarea {
      margin-bottom: 30px;
    }
  }
  .card-title {
    text-align: left;
    font-weight: 600;
  }
  .first {
    margin-bottom: 15px;
    text-align: left;
    // display: flex;
    // flex-direction: row;
    .first-title {
      display: flex;
      margin-bottom: 20px;
    }
    .left {
      margin-right: 40px;
      // float: left;
      // height: 100%;
    }
    .big-wrod {
      font-size: 24px;
      color: red;
    }
    .right {
      font-size: 14px;
      div {
        min-width: 140px;
        margin-right: 10px;
      }
      .big {
        font-weight: 600;
      }
    }
    .right-top {
      display: flex;
      margin-bottom: 10px;
    }
  }
  .red {
    color: red;
  }
  .confirms {
    margin-top: 15px;
  }
}
</style>
